package com.zry.fertilizer.ui.activity


import android.webkit.WebSettings
import com.zry.fertilizer.R
import com.zry.fertilizer.been.NoticeInfo
import com.zry.fertilizer.been.ReceiveNotice
import com.zry.fertilizer.been.ResultBean
import com.zry.fertilizer.been.SendOutNotice
import com.zry.fertilizer.databinding.ActivityMessageInfoBinding
import com.zry.fertilizer.retrofit.HttpServer
import com.zry.fertilizer.retrofit.ShowUserView
import com.zry.fertilizer.ui.base.BaseActivity
import kotlinx.android.synthetic.main.activity_message_info.*
import kotlinx.android.synthetic.main.common_title.*

class MessageInfoActivity : BaseActivity(), ShowUserView {


    private var receiveNotice: ReceiveNotice? = null
    private var messageInfoBinding: ActivityMessageInfoBinding? = null
    override fun getLayoutId(): Int {
        return R.layout.activity_message_info
    }

    override fun initData() {
        receiveNotice = intent.getSerializableExtra("ReceiveNotice") as ReceiveNotice
        titleCenterText.text = receiveNotice!!.NoticeTitle
        commonLeftLinearLayout.setOnClickListener { finish() }

        messageInfoBinding = viewDataBinding as ActivityMessageInfoBinding

        HttpServer(this).getNoticeInfo(receiveNotice!!.NoticeID, this)

        settingWebView()
    }

    override fun toMainActivity(what: Int, data: Any) {
        when (what) {
            44 -> {
                val resultData = data as ResultBean<NoticeInfo>
                if (resultData.State == "1") {
                    messageInfoBinding?.noticeInfo = resultData.ResultData
                    setWebViewContent(resultData.ResultData.NoticeBody)
                } else {
                    toast(resultData.Message)
                }
            }
        }
    }


    private fun settingWebView() {
        val webSetting = noticeContentWebView.settings
        webSetting.javaScriptEnabled = true
        noticeContentWebView.settings.useWideViewPort = true
        //自适应屏幕
        noticeContentWebView.settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        noticeContentWebView.settings.loadWithOverviewMode = true
        //改变字体大小

        webSetting.textZoom = 300

        //去掉滚动条
        noticeContentWebView.isHorizontalScrollBarEnabled = false//水平不显示
        noticeContentWebView.isVerticalScrollBarEnabled = false //垂直不显示
    }

    private fun setWebViewContent(content: String) {

        val webViewPadding = "<style type=\"text/css\">" + "body{padding-left: 20px;padding-right: 20px;" +
                "padding-top: 16px;padding-bottom: 16px;font-size:16px;}" + "</style>"

        noticeContentWebView.loadDataWithBaseURL(
                "about:blank",
                "<html lang=\"en\">\n" +
                        "<head>\n" +
                        "   <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>" +
                        webViewPadding +
                        "</head>\n" +
                        "<body>"
                        + content +
                        "</body>\n" +
                        "</html>",
                "text/html", "utf-8", null
        )
    }
}
